package com.company.huisuo.biz.activity

import android.app.Activity
import android.content.Intent
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.company.huisuo.R
import com.company.huisuo.base.BaseActivity
import com.company.huisuo.biz.adapter.MyOrderAdapter
import com.company.huisuo.biz.bean.ProductBean
import com.company.huisuo.databinding.ActivityMyOrderBinding


/**
 * 我的订单
 */
class MyOrderActivity : BaseActivity<ActivityMyOrderBinding>() {

    private val mAdapter by lazy {
        MyOrderAdapter()
    }

    companion object {
        fun start(activity: Activity) {
            activity.startActivity(Intent(activity, MyOrderActivity::class.java))
        }
    }

    override fun initView() {
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mBinding.tvSwitchAll.isSelected = true
        chooseTag(0)
        mBinding.tvSwitchAll.setOnClickListener {
            chooseTag(0)
        }
        mBinding.tvSwitchUndone.setOnClickListener {
            chooseTag(1)
        }
        mBinding.tvSwitchDone.setOnClickListener {
            chooseTag(2)
        }
        mBinding.rcvList.apply {
            layoutManager = GridLayoutManager(this@MyOrderActivity, 2)
            adapter = mAdapter
        }
    }

    override fun initData() {
        val list = arrayListOf<ProductBean>()
        list.add(ProductBean(
            id = "1",
            name = "芋圆桂花乌龙", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2500,
            marketPrice = 3000,
            stock = 100,
            salesCount = 50,
            deliveryTypes = emptyList()
        ))
        list.add(ProductBean(
            id = "2",
            name = "芋圆桂花乌龙", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2500,
            marketPrice = 3000,
            stock = 100,
            salesCount = 50,
            deliveryTypes = emptyList()
        ))
        list.add(ProductBean(
            id = "3",
            name = "黑糖波霸奶茶", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2800,
            marketPrice = 3200,
            stock = 100,
            salesCount = 40,
            deliveryTypes = emptyList()
        ))
        list.add(ProductBean(
            id = "4",
            name = "黑糖波霸奶茶", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2800,
            marketPrice = 3200,
            stock = 100,
            salesCount = 40,
            deliveryTypes = emptyList()
        ))
        list.add(ProductBean(
            id = "5",
            name = "芋圆酒香红茶", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2200,
            marketPrice = 2800,
            stock = 100,
            salesCount = 30,
            deliveryTypes = emptyList()
        ))
        list.add(ProductBean(
            id = "6",
            name = "芋圆酒香红茶", 
            introduction = "",
            categoryId = "",
            picUrl = "",
            sliderPicUrls = emptyList(),
            specType = false,
            price = 2200,
            marketPrice = 2800,
            stock = 100,
            salesCount = 30,
            deliveryTypes = emptyList()
        ))
        mAdapter.submitList(list)
    }

    private fun chooseTag(position: Int) {
        val param = mBinding.viewSwitchPoint.layoutParams as ConstraintLayout.LayoutParams
        when (position) {
            0 -> {
                param.startToStart = R.id.iv_switch_bg
                param.endToEnd = ConstraintLayout.LayoutParams.UNSET
            }
            1 -> {
                param.startToStart = R.id.iv_switch_bg
                param.endToEnd = R.id.iv_switch_bg
            }
            2 -> {
                param.startToStart = ConstraintLayout.LayoutParams.UNSET
                param.endToEnd = R.id.iv_switch_bg
            }
        }
        mBinding.viewSwitchPoint.layoutParams = param
        mBinding.tvSwitchAll.isSelected = position == 0
        mBinding.tvSwitchUndone.isSelected = position == 1
        mBinding.tvSwitchDone.isSelected = position == 2
    }
}